﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using PickListControl;
namespace AnyOffice.general.system.workflow.flow_type
{
    public partial class type_process_edit : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ListItem item;
                int num = (Request.QueryString["flow_id"] == null) ? 0 : int.Parse(Request.QueryString["flow_id"].ToString());
                this.ViewState["FlowId"] = num;
                bool flag = Request.QueryString["graph"] != null;
                this.ViewState["IsGraph"] = flag;
                if (flag)
                {
                    this.litButton.Text = "<input type=\"button\" value=\"关闭\" class=\"BigButton\" name=\"back\" onClick=\"window.close();\">";
                }
                else
                {
                    this.litButton.Text = "<input type=\"button\" value=\"返回\" class=\"BigButton\" name=\"back\" onClick=\"history.back();\">";
                }
                int ol = (Request.QueryString["id"] == null) ? 0 : int.Parse(Request.QueryString["id"].ToString());
                this.ViewState["Id"] = ol;
                FlowProcessDao dao = new FlowProcessDao();
                DataTable table = dao.SelectById(base.Conn, ol);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    if (flag)
                    {
                        this.litMessage.Text = "<input type=\"button\" value=\"关闭\" class=\"BigButton\" name=\"back\" onClick=\"window.close();\">";
                        return;
                    }
                    this.litMessage.Text = "<input type=\"button\" value=\"返回\" class=\"BigButton\" name=\"back\" onClick=\"history.back();\">";
                    return;
                }
                this.txtPrcsId.Text = table.Rows[0]["PrcsId"].ToString();
                this.txtPrcsName.Text = table.Rows[0]["PrcsName"].ToString();
                string str = (table.Rows[0]["PrcsTo"] != DBNull.Value) ? table.Rows[0]["PrcsTo"].ToString() : "";
                if (str.EndsWith(","))
                {
                    str = str.Substring(0, str.Length - 1);
                }
                DataTable table2 = dao.SelectByFlowId(base.Conn, num);
                if (str != string.Empty)
                {
                    string[] strArray = str.Split(new char[] { ',' });
                    if (strArray.Length > 0)
                    {
                        for (int i = 0; i < strArray.Length; i++)
                        {
                            if (strArray[i] == "0")
                            {
                                item = new ListItem("[结束流程]", "0");
                                this.pklMyTable.ItemsLeft.Add(item);
                            }
                            else
                            {
                                DataView view = new DataView(table2);
                                view.RowFilter = "PrcsId = " + strArray[i];
                                if (view.Count > 0)
                                {
                                    item = new ListItem(view[0]["PrcsId"].ToString() + "、" + view[0]["PrcsName"].ToString(), view[0]["PrcsId"].ToString());
                                    this.pklMyTable.ItemsLeft.Add(item);
                                }
                            }
                        }
                    }
                }
                str = "," + str + ",";
                if (table2.Rows.Count > 0)
                {
                    for (int j = 0; j < table2.Rows.Count; j++)
                    {
                        item = new ListItem(table2.Rows[j]["PrcsId"].ToString() + "、" + table2.Rows[j]["PrcsName"].ToString(), table2.Rows[j]["PrcsId"].ToString());
                        if (str.IndexOf("," + table2.Rows[j]["PrcsId"].ToString() + ",") < 0)
                        {
                            this.pklMyTable.ItemsRight.Add(item);
                        }
                    }
                }
                if (str.IndexOf(",0,") < 0)
                {
                    item = new ListItem("[结束流程]", "0");
                    this.pklMyTable.ItemsRight.Add(item);
                }
                if (this.ddlAttachmentFlag.Items.FindByValue(table.Rows[0]["AttachmentFlag"].ToString()) != null)
                {
                    this.ddlAttachmentFlag.Items.FindByValue(table.Rows[0]["AttachmentFlag"].ToString()).Selected = true;
                }
                if (this.Feedback.Items.FindByValue(table.Rows[0]["Feedback"].ToString()) != null)
                {
                    this.Feedback.Items.FindByValue(table.Rows[0]["Feedback"].ToString()).Selected = true;
                }
                if (this.SignLook.Items.FindByValue(table.Rows[0]["SignLook"].ToString()) != null)
                {
                    this.SignLook.Items.FindByValue(table.Rows[0]["SignLook"].ToString()).Selected = true;
                }
                if (this.TurnPriv.Items.FindByValue(table.Rows[0]["TurnPriv"].ToString()) != null)
                {
                    this.TurnPriv.Items.FindByValue(table.Rows[0]["TurnPriv"].ToString()).Selected = true;
                }
                if (table.Rows[0]["MailTo"] != DBNull.Value)
                {
                    this.MailTo.Value = table.Rows[0]["MailTo"].ToString();
                }
                if ((table.Rows[0]["MailToNames"] != DBNull.Value) && (table.Rows[0]["MailToNames"].ToString() != string.Empty))
                {
                    this.MailToName.Value = table.Rows[0]["MailToNames"].ToString() + ",";
                }
                if (this.UserFilter.Items.FindByValue(table.Rows[0]["UserFilter"].ToString()) != null)
                {
                    this.UserFilter.Items.FindByValue(table.Rows[0]["UserFilter"].ToString()).Selected = true;
                }
                if (this.AutoType.Items.FindByValue(table.Rows[0]["AutoType"].ToString()) != null)
                {
                    this.AutoType.Items.FindByValue(table.Rows[0]["AutoType"].ToString()).Selected = true;
                }
                if (this.UserLock.Items.FindByValue(table.Rows[0]["UserLock"].ToString()) != null)
                {
                    this.UserLock.Items.FindByValue(table.Rows[0]["UserLock"].ToString()).Selected = true;
                }
                this.TopDefault.Checked = Convert.ToBoolean(table.Rows[0]["TopDefault"].ToString());
                if (table.Rows[0]["AutoUserOp"] != DBNull.Value)
                {
                    this.AutoUserOp.Value = table.Rows[0]["AutoUserOp"].ToString();
                }
                if ((table.Rows[0]["AutoUserOpNames"] != DBNull.Value) && (table.Rows[0]["AutoUserOpNames"].ToString() != string.Empty))
                {
                    this.AutoUserOpName.Value = table.Rows[0]["AutoUserOpNames"].ToString() + ",";
                }
                if (table.Rows[0]["AutoUser"] != DBNull.Value)
                {
                    this.AutoUser.Value = table.Rows[0]["AutoUser"].ToString();
                }
                if ((table.Rows[0]["AutoUserNames"] != DBNull.Value) && (table.Rows[0]["AutoUserNames"].ToString() != string.Empty))
                {
                    this.AutoUserName.Value = table.Rows[0]["AutoUserNames"].ToString() + ",";
                }
                if (table.Rows[0]["Timeout"] != DBNull.Value)
                {
                    this.txtTimeout.Text = table.Rows[0]["Timeout"].ToString();
                }
                if (table.Rows[0]["Plugin"] != DBNull.Value)
                {
                    this.txtPlugin.Text = table.Rows[0]["Plugin"].ToString();
                }
            }
            if (this.Page.IsPostBack)
            {
                FlowProcessDao dao2 = new FlowProcessDao();
                FlowProcessData data = new FlowProcessData();
                data.Id = Convert.ToInt32(this.ViewState["Id"].ToString());
                data.PrcsId = Convert.ToInt32(this.txtPrcsId.Text);
                data.PrcsName = this.txtPrcsName.Text;
                string str2 = "";
                foreach (ListItem item2 in this.pklMyTable.ItemsLeft)
                {
                    if (str2 == string.Empty)
                    {
                        str2 = item2.Value;
                    }
                    else
                    {
                        str2 = str2 + "," + item2.Value;
                    }
                }
                if (str2 != string.Empty)
                {
                    data.PrcsTo = str2;
                }
                data.AttachmentFlag = Convert.ToInt32(this.ddlAttachmentFlag.SelectedValue);
                data.Feedback = Convert.ToInt32(this.Feedback.Value);
                data.SignLook = Convert.ToInt32(this.SignLook.Value);
                data.TurnPriv = Convert.ToInt32(this.TurnPriv.Value);
                if (this.MailTo.Value != string.Empty)
                {
                    data.MailTo = this.MailTo.Value;
                }
                data.UserFilter = Convert.ToInt32(this.UserFilter.Value);
                data.AutoType = Convert.ToInt32(this.AutoType.Value);
                data.UserLock = Convert.ToInt32(this.UserLock.Value);
                data.TopDefault = this.TopDefault.Checked;
                if (this.AutoUserOp.Value != string.Empty)
                {
                    data.AutoUserOp = this.AutoUserOp.Value;
                }
                if (this.AutoUser.Value != string.Empty)
                {
                    data.AutoUser = this.AutoUser.Value;
                }
                if (this.txtTimeout.Text != string.Empty)
                {
                    data.TimeOut = Convert.ToInt32(this.txtTimeout.Text);
                }
                if (this.txtPlugin.Text != string.Empty)
                {
                    data.Plugin = this.txtPlugin.Text;
                }
                dao2.SetBase(base.Conn, data);
                base.CloseConn();
                if (Convert.ToBoolean(this.ViewState["IsGraph"].ToString()))
                {
                    if (!this.Page.IsStartupScriptRegistered("SefClose"))
                    {
                        this.Page.RegisterStartupScript("SefClose", "<script>parent.opener.location.reload();\nwindow.close();</script>\n");
                    }
                }
                else
                {
                    Response.Redirect("type_view_list.aspx?flow_id=" + this.ViewState["FlowId"].ToString());
                }
            }
        }
    }
}
